package com.irdstudio.efp.esb.api.rest.yed.apply;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.api.rest.EsbController;
import com.irdstudio.efp.esb.api.rest.yed.req.YedApplyBackReqBean;
import com.irdstudio.efp.esb.api.service.yed.apply.YedApplyBackService;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 优e贷用信风控审批结果通知接口
 *
 * @author liangcongming :2020/7/30 18:29
 * @version 0.1
 * @date 2020/7/30 18:29
 * @since 2020/7/30 18:29
 */
@RestController
@RequestMapping("/esb")
public class YedApplyBackController implements EsbController {

    private static final Logger log = LoggerFactory.getLogger(YedApplyBackController.class);

    private static final String LOGGER_ID = "优e贷-用信风控结果通知";

    @Autowired
    private YedApplyBackService yedApplyBackService;

    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        log.info(LOGGER_ID + "获取ESB请求系统头和请求应用头对象!");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }

    @ModelAttribute("yedApplyBackReqBean")
    public YedApplyBackReqBean getReqEsbBodyBean(HttpServletRequest request) {
        log.info(LOGGER_ID + "获取 ESB请求体！");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, YedApplyBackReqBean.class);
    }

    @PostMapping("/SERVICE_30220007_10")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult bindingResult1,
                                     @Validated({GroupeInterface.yed.class}) @ModelAttribute("yedApplyBackReqBean") YedApplyBackReqBean yedApplyBackReqBean, BindingResult bindingResult2, Exception exception) {
        // 全局关联流水号
        String glblSrlNo = esbReqServiceBean.getSysHead().getGlblSrlNo();
        try {
            // 参数校验
            validateBean(esbReqServiceBean, bindingResult1, yedApplyBackReqBean, bindingResult2, exception, log);
            // 授信申请流水号
            String crdtAppFlowNo = yedApplyBackReqBean.getUseCrdtAppSrlNo();
            log.info("======>调用接口【" + glblSrlNo + "_30220007_10】开始<======");

            // 设置系统头对象
            setSysHeadFieldThreadLocal(esbReqServiceBean);

            yedApplyBackService.processApply(yedApplyBackReqBean);

            // 执行授信请求流程并返回
            return EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean);

        } catch (BizException e) {
            log.error("优e贷-用信风控结果通知BizException", e);
            return EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean, e.getErrorCode(), e.getMessage());
        } catch (ESBException e) {
            log.error("优e贷-用信风控结果通知ESBException", e);
            return EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean, e.getErrorCode(), e.getMessage());
        } finally {
            log.info("======>调用接口【" + glblSrlNo + "_30220007_10】结束<======");
        }
    }
}
